import 'package:flutter/material.dart';

abstract class BlocBase {

  Future? getData({required String labelId,required int page});

  Future? onRefresh({required String labelId});

  Future? onLoadMore({required String labelId});

  void dispose();
}

class BlocProvider<T extends BlocBase> extends StatefulWidget {BlocProvider({Key? key, required this.child, required this.bloc, this.userDispose: true}) : super(key: key);

  final T bloc;
  final Widget child;
  final bool userDispose;

  @override
  _BlocProviderState<T> createState() => _BlocProviderState<T>();

  static T of<T extends BlocBase>(BuildContext context) {
    BlocProvider<T> provider = context.findAncestorWidgetOfExactType<BlocProvider<T>>()!;
    return provider.bloc;
  }
}

class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
  @override
  void dispose() {
    // TODO: implement dispose
    if (widget.userDispose) widget.bloc.dispose();
    super.dispose();
  }
}